<!-- 表格组件 -->
<template>
  <div>
    <el-table :data="tableData" border style="width: 100%">
      <TableColumn
        v-for="(item, index) in tableConifg"
        :key="index"
        v-bind="item"
        @clickHandle="clickHandle"
      >
        <template #slot="{ row }" v-if="item.slot">
          <slot :name="item.slot" :row="row"></slot>
        </template>
      </TableColumn>
      <el-table-column
        v-if="operateProps.operateList.length"
        :label="operateProps.label"
        :width="operateProps.width"
        :fixed="operateProps.fixed"
      >
        <template #default="{ row }">
          <div class="operate-btn">
            <el-tooltip
              v-for="operateItem in operateProps.operateList"
              effect="dark"
              :content="operateItem.text"
              placement="top"
            >
              <el-button
                text
                :icon="operateItem.icon"
                @click="operateItem.handle(row)"
              ></el-button>
            </el-tooltip>
          </div>
        </template>
      </el-table-column>
      <template #empty>
        <div>暂无数据</div>
      </template>
    </el-table>
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      background
      :page-sizes="[10, 20, 30, 40, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="paginationProps.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts" setup>
import { ref, watch, defineProps, defineEmits } from "vue";
import TableColumn from "./TableColumn.vue";
import type { TableConfigItem } from "@com/Table/index";

const props = defineProps({
  tableData: {
    // 表格数据
    type: Array,
    default: () => [],
  },
  tableConifg: {
    // 数据列配置
    type: Array<TableConfigItem>,
    default: () => [],
  },
  operateProps: {
    //操作列配置
    type: Object,
    default: {
      label: "操作",
      width: 120,
      fixed: "right", //默认固定到右侧
      operateList: [], //操作列按钮组
    },
  },
  paginationProps: {
    //分页配置
    type: Object,
    default: {
      total: 0,
      currentPage: 1,
      pageSize: 10,
    },
  },
});
const emit = defineEmits([
  "handleSizeChange",
  "handleCurrentChange",
  "handleOpenView",
]);

const currentPage = ref(1);
const pageSize = ref(10);

//点击方法
const clickHandle = <T>(item: TableConfigItem, data: T) => {
  if (item.isOpenView) {
    handleOpenView(data);
  }
};

//打开详情
const handleOpenView = <T>(data: T) => {
  emit("handleOpenView", data);
};

watch(
  () => props.paginationProps.currentPage,
  (newVal: number) => {
    if (newVal !== currentPage.value) {
      currentPage.value = newVal;
    }
  }
);

watch(
  () => props.paginationProps.pageSize,
  (newVal: number) => {
    if (newVal !== pageSize.value) {
      pageSize.value = newVal;
    }
  }
);

const handleSizeChange = (val: number) => {
  pageSize.value = val;
  emit("handleSizeChange", pageSize.value);
};

const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  emit("handleCurrentChange", currentPage.value);
};
</script>
<style lang="scss" scoped>
.el-table {
  :deep(.el-table__header-wrapper) {
    .el-table__header {
      width: 100% !important;
    }
  }

  :deep(.el-table__body-wrapper) {
    .el-table__body {
      width: 100% !important;
      .operate-btn {
        text-align: center;
        .el-button {
          padding: 5px;
          font-size: 18px;
        }
      }
    }
  }
}

.el-pagination {
  justify-content: end;
  margin-top: 10px;
}
</style>
